package com.easydata.agent.core;

import com.easydata.libPlugin.core.AbstractDAGNode;
import com.easydata.libPlugin.core.AbstractDataIO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DAG {

    // key: node id
    HashMap<String, AbstractDAGNode> nodes = new HashMap<>();
    // key: node id
    // value: next node ids
    HashMap<String,List<String>> backwardRelation = new HashMap<>();
    // key: node id
    // value: previous node ids
    HashMap<String,List<String>> forwardRelation = new HashMap<>();


    public DAG(){
    }

    public void addNode(AbstractDAGNode node){
        nodes.put(node.getId(),node);
        backwardRelation.putIfAbsent(node.getId(), new ArrayList<>());
        forwardRelation.putIfAbsent(node.getId(), new ArrayList<>());
    }

    public void link(AbstractDAGNode from, AbstractDAGNode to){
        link(from.getId(),to.getId());
    }

    public void link(String fromId,String toId){

        if(backwardRelation.containsKey(fromId)){
            backwardRelation.get(fromId).add(toId);
        }

        if(forwardRelation.containsKey(toId)){
            forwardRelation.get(toId).add(fromId);
        }
    }

    public List<AbstractDAGNode> getNodes(){
        return nodes.values().stream().toList();
    }

    public AbstractDAGNode getNode(String id){
        return nodes.get(id);
    }

    public List<AbstractDAGNode> getBackwardNodes(String id){
        return backwardRelation.get(id).stream().map(x->{
            return nodes.get(x);
        }).toList();
    }

    public List<AbstractDAGNode> getForwardNodes(String id){
        return forwardRelation.get(id).stream().map(x->{
            return nodes.get(x);
        }).toList();
    }
}
